<template>
  <view class="profile-page">
    <!-- 用户信息头部 -->
    <view class="user-header">
      <view class="user-avatar-section">
        <image 
          :src="userInfo.avatar || '/static/images/default-avatar.png'" 
          class="user-avatar"
          @click="uploadAvatar"
        />
        <view class="user-status">
          <text class="status-tag" :class="userInfo.status === 1 ? 'status-active' : 'status-inactive'">
            {{ userInfo.status === 1 ? '正常' : '禁用' }}
          </text>
        </view>
      </view>
      
      <view class="user-info">
        <text class="user-name">{{ userInfo.username }}</text>
        <text class="user-role">{{ userInfo.roleName }}</text>
        <text class="user-phone">{{ userInfo.phone }}</text>
      </view>
    </view>

    <!-- 功能菜单列表 -->
    <view class="menu-list">
      <!-- 个人信息 -->
      <view class="menu-item" @click="goToUserInfo">
        <view class="menu-icon">
          <u-icon name="account" size="20" color="#667eea"></u-icon>
        </view>
        <view class="menu-content">
          <text class="menu-title">个人信息</text>
          <text class="menu-desc">查看、编辑个人资料</text>
        </view>
        <view class="menu-arrow">
          <u-icon name="arrow-right" size="16" color="#ccc"></u-icon>
        </view>
      </view>

      <!-- 修改密码 -->
      <view class="menu-item" @click="goToChangePassword">
        <view class="menu-icon">
          <u-icon name="lock" size="20" color="#667eea"></u-icon>
        </view>
        <view class="menu-content">
          <text class="menu-title">修改密码</text>
          <text class="menu-desc">修改登录密码</text>
        </view>
        <view class="menu-arrow">
          <u-icon name="arrow-right" size="16" color="#ccc"></u-icon>
        </view>
      </view>

      <!-- 我的权限 -->
      <view class="menu-item" @click="goToPermissions">
        <view class="menu-icon">
          <u-icon name="shield" size="20" color="#667eea"></u-icon>
        </view>
        <view class="menu-content">
          <text class="menu-title">我的权限</text>
          <text class="menu-desc">查看当前权限</text>
        </view>
        <view class="menu-arrow">
          <u-icon name="arrow-right" size="16" color="#ccc"></u-icon>
        </view>
      </view>

      <!-- 菜单权限 -->
      <view class="menu-item" @click="goToMenuPermissions">
        <view class="menu-icon">
          <u-icon name="list" size="20" color="#667eea"></u-icon>
        </view>
        <view class="menu-content">
          <text class="menu-title">菜单权限</text>
          <text class="menu-desc">查看菜单访问权限</text>
        </view>
        <view class="menu-arrow">
          <u-icon name="arrow-right" size="16" color="#ccc"></u-icon>
        </view>
      </view>

      <!-- 消息通知 -->
      <view class="menu-item" @click="goToNotifications">
        <view class="menu-icon">
          <u-icon name="bell" size="20" color="#667eea"></u-icon>
        </view>
        <view class="menu-content">
          <text class="menu-title">消息通知</text>
          <text class="menu-desc">通知设置、消息管理</text>
        </view>
        <view class="menu-arrow">
          <u-icon name="arrow-right" size="16" color="#ccc"></u-icon>
        </view>
      </view>

      <!-- 系统设置 -->
      <view class="menu-item" @click="goToSystemSettings">
        <view class="menu-icon">
          <u-icon name="setting" size="20" color="#667eea"></u-icon>
        </view>
        <view class="menu-content">
          <text class="menu-title">系统设置</text>
          <text class="menu-desc">应用设置、缓存管理</text>
        </view>
        <view class="menu-arrow">
          <u-icon name="arrow-right" size="16" color="#ccc"></u-icon>
        </view>
      </view>
    </view>

    <!-- 账户安全信息 -->
    <view class="security-section">
      <view class="section-title">账户安全</view>
      <view class="security-list">
        <view class="security-item">
          <view class="security-info">
            <text class="security-title">最近登录记录</text>
            <text class="security-desc">{{ loginRecords[0]?.loginTime || '暂无记录' }}</text>
          </view>
          <view class="security-status">
            <text class="status-text" :class="loginRecords[0]?.statusType || 'success'">
              {{ loginRecords[0]?.statusText || '正常' }}
            </text>
          </view>
        </view>
        
        <view class="security-item">
          <view class="security-info">
            <text class="security-title">登录设备</text>
            <text class="security-desc">{{ loginRecords[0]?.device || '未知设备' }}</text>
          </view>
        </view>
        
        <view class="security-item">
          <view class="security-info">
            <text class="security-title">登录地点</text>
            <text class="security-desc">{{ loginRecords[0]?.location || '未知地点' }}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 应用信息 -->
    <view class="app-section">
      <view class="section-title">应用信息</view>
      <view class="app-list">
        <view class="app-item">
          <text class="app-label">版本号</text>
          <text class="app-value">{{ appInfo.version }}</text>
        </view>
        <view class="app-item">
          <text class="app-label">构建时间</text>
          <text class="app-value">{{ appInfo.buildTime }}</text>
        </view>
        <view class="app-item">
          <text class="app-label">缓存大小</text>
          <text class="app-value">{{ appInfo.cacheSize }}</text>
        </view>
      </view>
    </view>

    <!-- 退出登录 -->
    <view class="logout-section">
      <button class="logout-btn" @click="handleLogout">
        <u-icon size="20" color="white"></u-icon>
        <text class="logout-btn-text">安全退出</text>
      </button>
    </view>
  </view>
</template>

<script setup>
/**
 * 用户管理页面 - 原始版本
 * @author Wu.Liang
 * @date 2025-01-21
 * @version 1.0 - 原始菜单列表样式
 */

import { ref, reactive, onMounted } from 'vue'
import { hasMenuPermission } from '@/utils/permission'
import { useAuthStore } from '@/store'



// 用户信息
const userInfo = reactive({
  username: '张三',
  roleName: '业主',
  phone: '138****1234',
  email: 'zhangsan@example.com',
  avatar: '/static/images/default-avatar.png',
  status: 1
})

// 登录记录
const loginRecords = ref([
  {
    id: 1,
    loginTime: '2025-01-21 14:30',
    device: 'iPhone 14',
    location: '北京市朝阳区',
    statusText: '成功',
    statusType: 'success'
  },
  {
    id: 2,
    loginTime: '2025-01-20 09:15',
    device: 'Android手机',
    location: '北京市海淀区',
    statusText: '成功',
    statusType: 'success'
  },
  {
    id: 3,
    loginTime: '2025-01-19 22:00',
    device: 'iPad',
    location: '北京市西城区',
    statusText: '成功',
    statusType: 'success'
  }
])

// 应用信息
const appInfo = reactive({
  version: 'v2.1.0',
  buildTime: '2025-01-15',
  cacheSize: '15.6MB'
})

// 页面跳转方法
const goToUserInfo = () => {
  uni.navigateTo({ url: '/pages/profile/edit' })
}

const goToChangePassword = () => {
  uni.navigateTo({ url: '/pages/profile/change-password' })
}

const goToPermissions = () => {
  uni.navigateTo({ url: '/pages/profile/permissions' })
}

const goToMenuPermissions = () => {
  uni.navigateTo({ url: '/pages/profile/menu-permissions' })
}

const goToNotifications = () => {
  uni.navigateTo({ url: '/pages/profile/notifications' })
}

const goToSystemSettings = () => {
  uni.navigateTo({ url: '/pages/profile/settings' })
}

// 上传头像
const uploadAvatar = () => {
  uni.chooseImage({
    count: 1,
    sizeType: ['compressed'],
    sourceType: ['album', 'camera'],
    success: (res) => {
      const tempFilePath = res.tempFilePaths[0]
      // 这里应该调用上传API
      console.log('选择头像:', tempFilePath)
      userInfo.avatar = tempFilePath
      uni.showToast({
        title: '头像上传成功',
        icon: 'success'
      })
    }
  })
}



// 退出登录
const handleLogout = () => {
  // 显示确认对话框
  uni.showModal({
    title: '安全退出',
    content: '确定要退出登录吗？退出后需要重新登录。',
    confirmText: '确定退出',
    cancelText: '取消',
    success: async (res) => {
      if (res.confirm) {
        console.log('用户确认退出登录')
        
        // 显示加载提示
        uni.showLoading({
          title: '正在退出...',
          mask: true
        })
        
        try {
          // 使用认证store进行退出登录
          const authStore = useAuthStore()
          await authStore.logout()
          
          // 隐藏加载提示
          uni.hideLoading()
          
          // 显示退出成功提示
          uni.showToast({
            title: '退出成功',
            icon: 'success',
            duration: 1500
          })
          
        } catch (error) {
          console.error('退出登录失败:', error)
          uni.hideLoading()
          
          // 即使失败也要清理本地数据并跳转
          const authStore = useAuthStore()
          authStore.clearAuth()
          
          uni.showToast({
            title: '退出成功',
            icon: 'success',
            duration: 1500
          })
          
          setTimeout(() => {
            uni.reLaunch({
              url: '/pages/login/login'
            })
          }, 1500)
        }
      }
    },
    fail: (error) => {
      console.error('显示确认对话框失败:', error)
      uni.showToast({
        title: '操作失败，请重试',
        icon: 'error'
      })
    }
  })
}







// 页面加载
onMounted(() => {
  // 加载用户信息
  loadUserInfo()
  // 加载登录记录
  loadLoginRecords()
  // 加载应用信息
  loadAppInfo()
})

// 数据加载方法
const loadUserInfo = async () => {
  try {
    // 这里应该调用实际的API
    console.log('加载用户信息')
  } catch (error) {
    console.error('加载用户信息失败:', error)
  }
}

const loadLoginRecords = async () => {
  try {
    // 这里应该调用实际的API
    console.log('加载登录记录')
  } catch (error) {
    console.error('加载登录记录失败:', error)
  }
}

const loadAppInfo = async () => {
  try {
    // 这里应该调用实际的API
    console.log('加载应用信息')
  } catch (error) {
    console.error('加载应用信息失败:', error)
  }
}
</script>

<style lang="scss" scoped>
.profile-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 0;
  padding-bottom: 100px; /* 为底部导航栏留出空间 */
}



.user-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px 20px 30px;
  color: white;
  text-align: center;
  
  .user-avatar-section {
    position: relative;
    display: inline-block;
    margin-bottom: 20px;
    
    .user-avatar {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      border: 3px solid rgba(255, 255, 255, 0.3);
    }
    
    .user-status {
      position: absolute;
      bottom: 0;
      right: 0;
      
      .status-tag {
        padding: 2px 8px;
        border-radius: 10px;
        font-size: 10px;
        font-weight: bold;
        
        &.status-active {
          background: #27ae60;
          color: white;
        }
        
        &.status-inactive {
          background: #e74c3c;
          color: white;
        }
      }
    }
  }
  
  .user-info {
    .user-name {
      display: block;
      font-size: 20px;
      font-weight: bold;
      margin-bottom: 5px;
    }
    
    .user-role {
      display: block;
      font-size: 14px;
      opacity: 0.8;
      margin-bottom: 5px;
    }
    
    .user-phone {
      display: block;
      font-size: 12px;
      opacity: 0.7;
    }
  }
}

.menu-list {
  background: white;
  margin: 15px;
  border-radius: 10px;
  overflow: hidden;
  
  .menu-item {
    display: flex;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid #f0f0f0;
    
    &:last-child {
      border-bottom: none;
    }
    
    &:active {
      background: #f8f9fa;
    }
    
    .menu-icon {
      width: 40px;
      height: 40px;
      border-radius: 8px;
      background: #f8f9fa;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 15px;
      
      .iconfont {
        font-size: 20px;
        color: #666;
      }
    }
    
    .menu-content {
      flex: 1;
      
      .menu-title {
        display: block;
        font-size: 16px;
        color: #333;
        margin-bottom: 3px;
      }
      
      .menu-desc {
        display: block;
        font-size: 12px;
        color: #999;
      }
    }
    
    .menu-arrow {
      .iconfont {
        font-size: 16px;
        color: #ccc;
      }
    }
  }
}

.security-section,
.app-section {
  background: white;
  margin: 15px;
  border-radius: 10px;
  padding: 20px;
  
  .section-title {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #f0f0f0;
  }
  
  .security-list,
  .app-list {
    .security-item,
    .app-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 12px 0;
      border-bottom: 1px solid #f8f9fa;
      
      &:last-child {
        border-bottom: none;
      }
      
      .security-info,
      .app-label {
        .security-title,
        .app-label {
          display: block;
          font-size: 14px;
          color: #333;
          margin-bottom: 3px;
        }
        
        .security-desc {
          display: block;
          font-size: 12px;
          color: #999;
        }
      }
      
      .security-status,
      .app-value {
        .status-text {
          padding: 2px 8px;
          border-radius: 10px;
          font-size: 10px;
          
          &.success {
            background: #d4edda;
            color: #155724;
          }
          
          &.warning {
            background: #fff3cd;
            color: #856404;
          }
          
          &.danger {
            background: #f8d7da;
            color: #721c24;
          }
        }
        
        .app-value {
          font-size: 14px;
          color: #666;
        }
      }
    }
  }
}



.logout-section {
  margin: 20px 15px 30px;
  
  .logout-btn {
    width: 100%;
    height: 55px;
    background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: 17px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(231, 76, 60, 0.3);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
      transition: left 0.5s;
    }
    
    .logout-btn-text {
      margin-left: 8px;
      font-weight: 600;
    }
    
    &:active {
      background: linear-gradient(135deg, #c0392b 0%, #a93226 100%);
      transform: translateY(2px);
      box-shadow: 0 2px 8px rgba(231, 76, 60, 0.3);
      
      &::before {
        left: 100%;
      }
    }
    
    &:hover {
      box-shadow: 0 6px 16px rgba(231, 76, 60, 0.4);
    }
  }
}



// 响应式适配
@media screen and (max-width: 375px) {
  .user-header {
    padding: 30px 15px 25px;
  }
  
  .menu-list,
  .security-section,
  .app-section {
    margin: 10px;
  }
}

@media screen and (min-width: 376px) and (max-width: 768px) {
  .user-header {
    padding: 40px 20px 30px;
  }
  
  .menu-list,
  .security-section,
  .app-section {
    margin: 15px;
  }
}

@media screen and (min-width: 769px) {
  .profile-page {
    max-width: 600px;
    margin: 0 auto;
  }
  
  .user-header {
    padding: 50px 30px 40px;
  }
  
  .menu-list,
  .security-section,
  .app-section {
    margin: 20px;
  }
}
</style> 